package cn.iocoder.yudao.module.trade.verify.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.ShopSimpleVO;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.trade.verify.dal.dataobject.TradeVerifyRecordDO;
import cn.iocoder.yudao.module.trade.verify.service.TradeVerifyRecordService;
import cn.iocoder.yudao.module.trade.verify.vo.TradeVerifyRecordPageReqVO;
import cn.iocoder.yudao.module.trade.verify.vo.TradeVerifyRecordRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 核销记录")
@RestController
@RequestMapping("/trade/verify-record")
@Validated
public class TradeVerifyRecordController {

    @Resource
    private TradeVerifyRecordService verifyRecordService;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private ShopInfoApi shopInfoApi;

    @GetMapping("/get")
    @Operation(summary = "获得核销记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:verify-record:query')")
    public CommonResult<TradeVerifyRecordRespVO> getVerifyRecord(@RequestParam("id") Long id) {
        TradeVerifyRecordDO verifyRecord = verifyRecordService.getVerifyRecord(id);
        Assertions.notNull(verifyRecord, "核销记录不存在");
        TradeVerifyRecordRespVO respVO = BeanUtils.toBean(verifyRecord, TradeVerifyRecordRespVO.class);
        respVO.setUser(memberUserApi.getUser(verifyRecord.getUserId()));
        respVO.setShop(shopInfoApi.getShopInfoSimple(verifyRecord.getShopId()));
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得核销记录分页")
    @PreAuthorize("@ss.hasPermission('trade:verify-record:query')")
    public CommonResult<PageResult<TradeVerifyRecordRespVO>> getVerifyRecordPage(@Valid TradeVerifyRecordPageReqVO pageReqVO) {
        PageResult<TradeVerifyRecordDO> pageResult = verifyRecordService.getVerifyRecordPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        Set<Long> userIds = convertSet(pageResult.getList(), TradeVerifyRecordDO::getUserId);
        Set<Long> shopIds = convertSet(pageResult.getList(), TradeVerifyRecordDO::getShopId);
        Map<Long, UserSimpleVO> users = memberUserApi.getUserSimpleMap(userIds);
        Map<Long, ShopSimpleVO> shops = shopInfoApi.getShopSimpleMap(shopIds);
        PageResult<TradeVerifyRecordRespVO> data = BeanUtils.toBean(pageResult, TradeVerifyRecordRespVO.class);
        data.getList().forEach(item -> {
            item.setUser(users.getOrDefault(item.getUserId(), UserSimpleVO.ANONYMOUS));
            item.setShop(shops.getOrDefault(item.getShopId(), ShopSimpleVO.EMPTY));
        });
        return success(data);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出核销记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:verify-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVerifyRecordExcel(@Valid TradeVerifyRecordPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TradeVerifyRecordRespVO> list = getVerifyRecordPage(pageReqVO).getData().getList();
        // 导出 Excel
        ExcelUtils.write(response, "核销记录.xls", "数据", TradeVerifyRecordRespVO.class, list);
    }

}